<template>
  <div class="sidebar-section">
    <h3 class="sidebar-title">24小时新闻热点</h3>
    <div class="hot-news">
      <div 
        class="news-item" 
        v-for="(news, index) in hotNews" 
        :key="index" 
        @click="handleNewsClick(news)"
        :class="{
          'top-1': index === 0,
          'top-2': index === 1,
          'top-3': index === 2
        }"
      >
        <span class="news-rank">{{ index + 1 }}</span>
        <span class="news-title">{{ news.title }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const hotNews = ref([]) 

const fetchHotNews = async () => {
  const response = await fetch('/app/contents/hot')
  const data = await response.json()
  console.log(data.data)
  hotNews.value = data.data
}

onMounted(() => {
  fetchHotNews()
})

const handleNewsClick = (news) => {
  console.log(news.contentId);
  router.push({ name: 'ContentDetail', params: { contentId: news.contentId } })
}
</script>

<style scoped>
.sidebar-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  
  .sidebar-title {
    margin-top: 0;
    margin-bottom: 15px;
    font-size: 16px;
    color: #333;
    padding-bottom: 8px;
    border-bottom: 1px solid #eee;
  }
}

.hot-news {
  .news-item {
    display: flex;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px dashed #eee;
    transition: all 0.2s ease;
    
    &:last-child {
      border-bottom: none;
    }
    
    .news-rank {
      width: 24px;
      height: 24px;
      line-height: 24px;
      text-align: center;
      background-color: #f5f7fa;
      border-radius: 4px;
      margin-right: 10px;
      font-size: 12px;
      color: #666;
      font-weight: normal;
      transition: all 0.2s ease;
    }
    
    .news-title {
      flex: 1;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      font-size: 14px;
      transition: all 0.2s ease;
      
      &:hover {
        color: var(--el-color-primary);
        cursor: pointer;
      }
    }
    
    &.top-1 {
      .news-rank {
        background-color: #ff4d4f;
        color: white;
        font-weight: bold;
      }
      .news-title {
        font-weight: bold;
        color: #ff4d4f;
      }
    }
    
    &.top-2 {
      .news-rank {
        background-color: #fa8c16;
        color: white;
        font-weight: bold;
      }
      .news-title {
        font-weight: bold;
        color: #fa8c16;
      }
    }
    
    &.top-3 {
      .news-rank {
        background-color: #faad14;
        color: white;
        font-weight: bold;
      }
      .news-title {
        font-weight: bold;
        color: #faad14;
      }
    }
    
    &:hover {
      transform: translateX(3px);
      
      .news-rank {
        transform: scale(1.1);
      }
    }
  }
}
</style>